package persistencia;


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class DACBase {
	
	private Connection conn;
	private Statement stm;

	
	public DACBase(){
	}
	
	public DACBase(DACBase dacBase){
		this.conn  = dacBase.getConn();		
	}
	
	public void abrirConexion(){
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			
			MysqlDataSource ds = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
			ds.setServerName("localhost/tesis?");
			ds.setPortNumber(3306);
			ds.setUser("root");
			ds.setPassword("admin");
			
			DataSource d = ds; 
			conn = d.getConnection();
 			
			
			//conn = DriverManager.getConnection("jdbc:mysql://localhost/dlc?" + 
		    //        "user=root&password=admin");
			
		}
	    catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
	    }
	    catch (Exception ex) {
         // handle the error
	    }
	}
	
	public void cerrarConexion(){
		try {
			conn.close();
		} catch (SQLException ex) {
			 // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
	    }
	}


	public Connection getConn() {
		return conn;
	}


	public void setConn(Connection conn) {
		this.conn = conn;
	}


	public Statement getStm() {
		try{
			stm = conn.createStatement();	    
		}
		catch (SQLException ex) {
	    // handle any errors
	    System.out.println("SQLException: " + ex.getMessage());
	    System.out.println("SQLState: " + ex.getSQLState());
	    System.out.println("VendorError: " + ex.getErrorCode());
		}
		return stm;
	}


	public void setStm(Statement stm) {
		this.stm = stm;
	}		    
}
